package cv.gzd.robot.controller;

import cv.gzd.common.utils.AjaxMsg;
import cv.gzd.common.utils.Page;
import cv.gzd.robot.pojo.Dev;
import cv.gzd.robot.pojo.vo.DevQueryVo;
import cv.gzd.robot.pojo.vo.ResultVo;
import cv.gzd.robot.service.DevService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * 设备控制器
 *
 * @author Jun
 */
@Controller
public class DevController {
    private static final HashMap<Integer, String> devmap;

    static {
        devmap=new HashMap<>(10);
    }

    @Autowired
    DevService devService;

    /**
     * 显示设备列表
     *
     * @param model
     * @param devVo
     * @return
     */
    @RequestMapping("/dev/list")
    public String devList(Model model, DevQueryVo devVo) {
        Page<Dev> page = devService.selectPageByDevQueryVo(devVo);
        model.addAttribute("page", page);
        model.addAttribute("QueryType", devVo.getQueryType());
        model.addAttribute("robotValue", devVo.getRobotValue());

        return "dev";
    }

    /**
     * 修改设备信息界面参数回调
     *
     * @param id
     * @return
     */
    @RequestMapping("/dev/edit.action")
    public @ResponseBody
    Dev editDev(Integer id) {
        return devService.selectDevByRiv(id);
    }


    /**
     * 保存修改设备信息
     *
     * @param dev
     * @return
     */
    @RequestMapping("/dev/update.action")
    public @ResponseBody
    ResultVo updateDev(Dev dev) {

        devService.updateDev(dev);
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(AjaxMsg.SUCCESS.getCode());
        return resultVo;
    }

    /**
     * 删除设备信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/dev/delete.action")
    public @ResponseBody
    ResultVo deleteDev(Integer id) {
        devService.deleteDev(id);
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(AjaxMsg.SUCCESS.getCode());
        return resultVo;
    }

    /**
     * 保存设备信息
     *
     * @param dev
     * @return
     */
    @RequestMapping("/dev/save.action")
    public @ResponseBody
    ResultVo saveDev(Dev dev) {
        devService.saveDev(dev);
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(AjaxMsg.SUCCESS.getCode());
        return resultVo;
    }
    /**
     * 连接设备
     *
     * @param id
     * @param devSession
     * @return
     */
    @RequestMapping("/dev/contect.aciton")
    public @ResponseBody
    String devContect(Integer id, HttpSession devSession) {
        Dev dev = devService.selectDevByRiv(id);
        devSession.setAttribute("rid", id);
        String robotname=dev.getRobotname();
        devSession.setAttribute("robotname", robotname);
        devmap.put(id,robotname);
        return "ok";
    }

    /**
     * 断开设备
     *
     * @param id
     * @param devSession
     * @return
     */
    @RequestMapping("/dev/discontect.aciton")
    public @ResponseBody
    String devDiscontect(Integer id, HttpSession devSession) {
        devmap.remove(id);
        return "ok";
    }
    @RequestMapping("/index.action")
    public String index(HttpSession session){

        return "index";
    }

    @RequestMapping("/indexInfo.action")
    public String indexInfo(Model model,Integer id,HttpSession session){
        model.addAttribute("rid",id);
        session.setAttribute("rid",id);
        return "index";
    }

    /**
     * 在线设备显示
     * @param session
     * @return
     */
    @RequestMapping("/onlinedevs")
    @ResponseBody
    public Set<Integer> onlinedevs(HttpSession session){
        Set<Integer> robotnameSet=new HashSet<>();
        for(Integer key:devmap.keySet()){
            robotnameSet.add(key);
        }
        return robotnameSet;
    }
    /**
     * 发送控制信息信息
     * @param rid
     * @return
     */
    @RequestMapping("/robotInfo.action")
    @ResponseBody
    public String robotInfo(@RequestParam("rid")String rid){
        return rid;
    }
}
